package net.fukure.android.pecabc;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Handler;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Gui {
	Button broadcastButton;
	EditText descriptionEditText;
	EditText chnameEditText;
	EditText genreEditText;
	EditText contactEditText;
	TextView liveTextView;
	Spinner bitrateSpinner;
	Button encodeButton;
	Dialog encodeDialog;
	EditText bbsUrlEditText;
	EditText peercastHostEditText;
	EditText peercastPasswordEditText;
	TextView linkMyTextView;
	TextView linkJavaCVTextView;
	CheckBox bbsAutoReloadCheckBox;
	CheckBox useRemoteCheckBox;
	CheckBox useTtsCheckBox;
	ViewGroup cameraViewLayout;
	
	AlertDialog broadcastDialog;
	AlertDialog bbsInputDialog;
	AlertDialog settingDialog;
	AlertDialog licenseDialog;
	ProgressBar loadingBar;
	
	Handler handler;
	Thread fpsThread;
	MainActivity activity;
	String status = "Live";
	
	Gui(MainActivity activity){
		this.activity = activity;
		this.handler = new Handler();
		
		init();
	}
	
	void init(){
		broadcastButton = getButton(R.id.broadcastButton);
		broadcastButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.mainHandler.onBroadcastButtonClick();
			}
		});
		
		encodeButton = getButton(R.id.encodeButton);
		encodeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.mainHandler.onEncodeButtonClick();
			}
		});
		
		liveTextView = getTextView(R.id.liveTextView);
		liveTextView.setVisibility(View.GONE);
		
		loadingBar = getProgressBar(R.id.bbsLoadingProgressBar);
		loadingBar.setVisibility(View.GONE);
		
		setButtonEnable(encodeButton, false);
		setButtonEnable(broadcastButton, false);
	}
	
	public void setBroadcastState(boolean state){
		if(state){
			status = "Broadcast";
		}else{
			status = "Live";
		}
	}
	
	void startFpsThread() {
		if(fpsThread!=null){
			fpsThread.interrupt();
		}
		fpsThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					while(true){
						Thread.sleep(1000);
						int fps = activity.getFps();
						setTextViewText(liveTextView, fps+"fps "+status);
					}
				} catch (InterruptedException e) {
				}
			}
		});
		fpsThread.start();
	}

	void stopFpsThread(){
		if(fpsThread!=null){
			fpsThread.interrupt();
		}
	}

	void setVisibility(final View view, final boolean visible){
		handler.post(new Runnable(){
			@Override
		    public void run() {
				if(visible){
					view.setVisibility(View.VISIBLE);
				}else{
					view.setVisibility(View.GONE);
				}
		    }
		});
	}
	
	void setTextViewText(final TextView textView, final String text){
		handler.post(new Runnable(){
			@Override
		    public void run() {
				textView.setText(text);
		    }
		});
	}
	
	void setButtonText(final Button button, final String text){
		handler.post(new Runnable(){
			@Override
		    public void run() {
				button.setText(text);
		    }
		});
	}
	
	void setButtonEnable(final Button button, final boolean enable){
		handler.post(new Runnable(){
			@Override
		    public void run() {
				button.setEnabled(enable);
		    }
		});
	}
	
	void toast(final String msg){
		handler.post(new Runnable(){
			@Override
		    public void run() {
				Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
		    }
		});
	}
	
	void toast(final int id){
		toast(activity.getResources().getString(id));
	}
	
	//private View getContentView(){
	//	return ((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
	//}

	Button getButton(int id) {
		return (Button) activity.findViewById(id);
	}

	TextView getTextView(int id){
		return (TextView) activity.findViewById(id);
	}

	EditText getEditText(int id){
		return (EditText) activity.findViewById(id);
	}
	
	CheckBox getCheckBox(int id){
		return (CheckBox) activity.findViewById(id);
	}
	
	ProgressBar getProgressBar(int id){
		return (ProgressBar) activity.findViewById(id);
	}
	
	Spinner getSpinner(int id){
		return (Spinner) activity.findViewById(id);
	}
	
	SurfaceView getSurfaceView(int id){
		return (SurfaceView) activity.findViewById(id);
	}
	
	ViewGroup getViewGroup(int id){
		return (ViewGroup) activity.findViewById(id);
	}

	Button getButton(View view, int id) {
		return (Button) view.findViewById(id);
	}

	TextView getTextView(View view, int id){
		return (TextView) view.findViewById(id);
	}
	
	EditText getEditText(View view, int id){
		View et = view.findViewById(id);
		return (EditText) et;
	}

	CheckBox getCheckBox(View view, int id){
		return (CheckBox) view.findViewById(id);
	}
	
	ProgressBar getProgressBar(View view, int id){
		return (ProgressBar) view.findViewById(id);
	}
	
	Spinner getSpinner(View view, int id){
		return (Spinner) view.findViewById(id);
	}
	
	SurfaceView getSurfaceView(View view, int id){
		return (SurfaceView) view.findViewById(id);
	}
	
	ViewGroup getViewGroup(View view, int id){
		return (ViewGroup) view.findViewById(id);
	}
}
